/**
 * 全站路由配置
 *
 * meta参数说明
 * keepAlive是否缓冲页面
 * isTab是否加入到tag导航
 * isAuth是否需要授权
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import PageRouter from './page/'
import ViewsRouter from './views/'
import AvueRouter from './avue-router'
import i18n from '@/lang'// Internationalization
import Store from '../store/'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, resolve, reject) {
    if (resolve || reject) return originalPush.call(this, location, resolve, reject)
    return originalPush.call(this, location).catch((e) => {})
}

Vue.use(VueRouter)
const Router = new VueRouter({
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      if (from.meta.keepAlive) {
        from.meta.savedPosition = document.body.scrollTop
      }
      return {
        x: 0,
        y: to.meta.savedPosition || 0
      }
    }
  },
  base: process.env.VUE_APP_BASE_URL,
  mode: 'history',
  // mode: 'hash',
  routes: [...PageRouter, ...ViewsRouter]
})

AvueRouter.install({
  store: Store,
  router: Router,
  i18n: i18n,
  keepAlive: false
})
Router.$avueRouter.formatRoutes(Store.state.auth.menuAll, true)
export default Router
